<?PHP
include_once "ini.php";
$id = $_POST['id'];
$id_user = $_POST['id_user'];
dbconnect();
$sql = "SELECT * FROM `images` WHERE id_image = '".$id."';";
$requete = mysql_query($sql);
if (!$requete)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{

    while ($tableau = mysql_fetch_assoc($requete))
    {
        $position = $_POST['position'];
        $dir = $_POST['dir'];
        $up = $_POST['up'];
        $tab = split(',', $position);

        if ($position != "")
        {
            $sql2 = "UPDATE `images` SET `coord_x` = '".$tab[0]."', coord_y = '".$tab[1]."', coord_z = '".$tab[2]."', orientation_dir = '".$dir."', orientation_up = '".$up."', posted = 'oui' WHERE `images`.`id_image` = '".$tableau['id_image']."' LIMIT 1 ;";
            $requete2 = mysql_query($sql2);
            if (!$requete2)
            {
                echo "SQL error : ", $sql2;
                exit ;
            }
            else
            {
                echo "save position done \r\n";

                $query2 = "SELECT * FROM quadrillage WHERE xmin < '".$tab[0]."' AND xmax > '".$tab[0]."' AND zmin < '".$tab[2]."' AND zmax > '".$tab[2]."'";
               
                $req = mysql_query($query2);

                if (!$req)
                {
                    echo "SQL error : ", $query2;
                    exit ;
                }
                else
                {
                	
                    while ($res = mysql_fetch_assoc($req))
                    {
                       	$sommex = $res['somme_antx']+$tab[0];
                        $sommez = $res['somme_antz']+$tab[2];
                        $nb_point = $res['nb_point']+1;
                        $barycentre = $sommex/$nb_point;
                        $barycentre .= ",0,";
                        $barycentre .= $sommez/$nb_point;
                        $sql3 = "UPDATE `quadrillage` SET `somme_antx` = '".$sommex."', somme_antz = '".$sommez."', nb_point = '".$nb_point."', barycentre = '".$barycentre."' WHERE `quadrillage`.`id_case` = '".$res['id_case']."' LIMIT 1 ;";
                        $requete3 = mysql_query($sql3);
                        if (!$requete3)
                        {
                            echo "SQL error : ", $sql3;
                            exit ;
                        }
                        else
                        {
                            echo "update position done \r\n";

                        }
                        $sql = "SELECT * FROM quadrillage;";
                        $requ = mysql_query($sql);

                        if (!$requ)
                        {
                            echo "SQL error : ", $sql;
                            exit ;
                        }
                        else
                        {
                            $file = "../quadrillage.txt";
                            $fp = fopen($file, "w");

                            while ($tabl = mysql_fetch_assoc($requ))
                            {
                                $ligne = $tabl['id_case']."	".$tabl['nb_point']."	".$tabl['barycentre']."\n";
                                fputs($fp, $ligne);
                            }
                            fclose($fp);
                        }
                    }

                }
            }
        }

        else
        {
            echo "pas de position";
        }
    }
}
?>
